package cn.xlmdz.subsoilerinfoquery.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kevin.delegationadapter.extras.ClickableAdapterDelegate;

import org.jetbrains.annotations.NotNull;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import cn.xlmdz.subsoilerinfoquery.R;
import cn.xlmdz.subsoilerinfoquery.bean.CarDataBean;
import cn.xlmdz.subsoilerinfoquery.databinding.ListCarItemBinding;
import cn.xlmdz.subsoilerinfoquery.ui.MainActivity;
import cn.xlmdz.subsoilerinfoquery.ui.QuerySingleActivity;

public class CarListAdapter extends ClickableAdapterDelegate<CarDataBean.Data, CarListAdapter.ShowContentAdapterVH> {
    private Context mContext;
    private int mItemType;

    public CarListAdapter(Context Context, int itemType) {
        this.mContext = Context;
        this.mItemType = itemType;
    }

    @NotNull
    @Override
    public ShowContentAdapterVH onCreateViewHolder(@NotNull ViewGroup viewGroup) {
        ListCarItemBinding listItemBinding = ListCarItemBinding.inflate(LayoutInflater.from(viewGroup.getContext()), viewGroup, false);
        return new ShowContentAdapterVH(listItemBinding);
    }

    @Override
    public void onBindViewHolder(@NotNull ShowContentAdapterVH holder, int position, CarDataBean.Data item) {
        super.onBindViewHolder(holder, position, item);

        //标题
        String number = item.getNumber();
        holder.listItemBinding.tvCarNumber.setText(number);

        //终端号码
        String terminalId = item.getTerminal_id();
        holder.listItemBinding.tvCarTerminalId.setText(terminalId);

        //ICCID
        String iccid = item.getIccid();
        holder.listItemBinding.tvCarIccid.setText(iccid);

        //车辆类型
        String type = item.getType();
        holder.listItemBinding.tvCarType.setText(type);

        //车主名称
        String name = item.getName();
        holder.listItemBinding.tvCarName.setText(name);

        //上线时间
        String gpsTime = item.getGps_time();
        holder.listItemBinding.tvCarGpsTime.setText(gpsTime);

        //状态
        try {
            Calendar calendar = Calendar.getInstance();
            if (!TextUtils.isEmpty(item.getGps_time())) {
                calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(item.getGps_time()));
            } else {
                calendar.setTimeInMillis(0);
            }
            boolean isOnline = (System.currentTimeMillis() - calendar.getTimeInMillis()) < (1000 * 60 * 30);
            if (isOnline) {
                holder.listItemBinding.tvCarState.setText("已上线");
                holder.listItemBinding.tvCarState.setTextColor(mContext.getResources().getColor(R.color.state_green));
            } else {
                holder.listItemBinding.tvCarState.setText("未上线");
                holder.listItemBinding.tvCarState.setTextColor(mContext.getResources().getColor(R.color.state_red));
            }
        } catch (ParseException e) {
            holder.listItemBinding.tvCarState.setText("未上线");
            holder.listItemBinding.tvCarState.setTextColor(mContext.getResources().getColor(R.color.state_red));
        }
    }

    @Override
    public void onItemClick(@NotNull View view, CarDataBean.Data item, int position) {
        super.onItemClick(view, item, position);
        Intent intent = new Intent(mContext, QuerySingleActivity.class);
        intent.putExtra("terminalId", item.getTerminal_id());
        mContext.startActivity(intent);
    }

    public static class ShowContentAdapterVH extends BaseViewHolder {
        ListCarItemBinding listItemBinding;

        public ShowContentAdapterVH(ListCarItemBinding listItemBinding) {
            super(listItemBinding.getRoot());

            this.listItemBinding = listItemBinding;
        }
    }
}
